package com.gjintel.framework.holidayset.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gjintel.framework.holidayset.dao.IHolidaySetDao;
import com.gjintel.framework.holidayset.model.HolidaySet;
import com.gjintel.framework.holidayset.service.IHolidaySetService;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.user.model.User;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.base.service.impl.BaseServiceImpl;

@Service("holidaySetService")
public class HolidaySetServiceImpl extends BaseServiceImpl implements
        IHolidaySetService {

    @Autowired
    private IHolidaySetDao holidaySetDao;

    @Override
    public void add(HolidaySet holidaySet) {
        this.holidaySetDao.add(holidaySet);
    }

    @Override
    public void updateByDate(String date, String userName, String descript) {

        this.holidaySetDao.updateByDate(date, userName, descript);
    }

    @Override
    public List<String> findAllByYear(String thisYear) {
        Integer tempYear = Integer.valueOf(thisYear);
        //String nextYear = ((Integer)(tempYear)).toString();
        return this.holidaySetDao.findAllByYear((tempYear - 1) + "", tempYear + 1 + "");
    }


    @Override
    public List<Date> getDays(String startDate, String endDate) {
        Date dateStar = null;
        Date dateEn = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dateStar = sdf.parse(startDate);
            dateEn = sdf.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendarStar = Calendar.getInstance();
        Calendar calendarEn = Calendar.getInstance();
        calendarStar.setTime(dateStar);
        calendarEn.setTime(dateEn);
        return this.getDatasByDate(calendarStar, calendarEn);
    }

    @Override
    public void saveWeekendsOfYear(String year, String createName) {
        int yeari = Integer.parseInt(year);
        List<String> weekends = this.getWeekends(yeari);
        List<String> dates = this.findByYear(year);
        for (String day : weekends) {
            if (!dates.contains(day)) {
                HolidaySet holidaySet = new HolidaySet();
                holidaySet.setAt_day(day);
                holidaySet.setAt_year(year);
                //	holidaySet.setDescript("双休");
                holidaySet.setUpdateTime(new Date());
                holidaySet.setCreate_name(createName);
                holidaySet.setHoilday_type(3);//双休
                this.add(holidaySet);
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void findAll(Page page, HolidaySet holidaySet, Sort sort) {
        this.holidaySetDao.findAll(page, holidaySet, sort);
    }

    @Override
    public Date getWorkDayByDays(Date startDate, int workdays) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy");
        Calendar calStart = Calendar.getInstance();
        calStart.setTime(startDate);
        List<String> dates = this.findAllByYear(sdf2.format(startDate));
        int tempDay = workdays;
        while (workdays >= 0) {
//			if(workdays!=0){
            if (dates.contains(sdf1.format(calStart.getTime()))) {
                calStart.add(Calendar.DAY_OF_YEAR, 1);
                if (tempDay == workdays) {//当前时间为第一天且为工作日
                    workdays--;
                }
            } else {
                calStart.add(Calendar.DAY_OF_YEAR, 1);
                workdays--;
            }
//			}
//			else{//最后一天
//				calStart.add(Calendar.DAY_OF_YEAR, 1);
//				workdays--;
//			}
        }
        calStart.set(Calendar.DATE, calStart.get(Calendar.DATE) - 1);
        return calStart.getTime();
    }

    @Override
    public void removeData(String deleteIds, User user) {
        HolidaySet holidaySet = null;
        for (String id : deleteIds.split(",")) {
            holidaySet = this.get(HolidaySet.class, id);
            if (holidaySet != null) {
                holidaySet.setDelete_flag(1);
                holidaySetDao.deleteById(id);
                LogUtil.addLog(new LogInfo("删除节假日信息", user.getId(), user.getName(), String.valueOf(LogType.DELETE.ordinal()), HolidaySet.class.getSimpleName(), id));
            }
        }
    }

    @Override
    public List<Date> getDatasByDate(Calendar pStart, Calendar pEnd) {
        List<Date> result = new ArrayList<Date>();
        while (pStart.before(pEnd)) {
            result.add(pStart.getTime());
            pStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        if (pStart.equals(pEnd)) {
            result.add(pStart.getTime());
        }
        return result;
    }

    @Override
    public List<String> getWeekends(int year) {
        List<String> result = new ArrayList<String>();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.set(year, 0, 1);

        for (int day = 1; day <= cal.getActualMaximum(Calendar.DAY_OF_YEAR); day++) {
            cal.set(Calendar.DAY_OF_YEAR, day);

            int weekDay = cal.get(Calendar.DAY_OF_WEEK);
            if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) {
                result.add(sdf.format(cal.getTime()));
            }
        }
        return result;
    }

    @Override
    public HolidaySet getHolidaySetbyDate(String date) {

        return holidaySetDao.getHolidaySetbyDate(date);
    }

    @Override
    public List<String> findByYear(String thisYear) {
        Integer tempYear = Integer.valueOf(thisYear);
        String nextYear = ((Integer) (tempYear + 1)).toString();
        return holidaySetDao.findByYear(thisYear, nextYear);
    }
}
